/*
 * Copyright 2008 Nordic Consulting & Development Company
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pl.ncdc.web.login.client.ui;

import pl.ncdc.web.login.client.i18n.LoginConstants;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasFocus;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.SuggestOracle;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

/**
 * Widget handling user credentials input.
 * <p>
 * Created on Apr 17, 2008
 *
 * @author hshsce
 * @version $Id: UserCredentialsWidget.java 174 2009-01-29 23:25:07Z hshsce $
 */
public class UserCredentialsWidget extends Composite {

	public static final String WIDGET_ID = "ncdc-web-login-UserCredentialsWidget";

	public interface Style {

		String LABEL = "Label";

		String TEXT = "Text";

	}

	private static final LoginConstants CONSTANTS = (LoginConstants) GWT.create(LoginConstants.class);

	private final Grid m_grid = new Grid(2, 1);

	private final TextBox m_uidTextBox = new TextBox();

	private final SuggestBox m_uidText;

	private final PasswordTextBox m_passwordText = new PasswordTextBox();

	private String m_uid;

	private CredentialsListenerCollection m_listeners;

	private boolean m_firstUidChangeAttempt = true;

	/**
	 * Creates user credentials widget instance.
	 *
	 * @param oracle the user suggest oracle.
	 */
	public UserCredentialsWidget(final SuggestOracle oracle) {
		if (oracle == null) {
			throw new IllegalArgumentException("oracle cannot be null");
		}
		m_uidText = new SuggestBox(oracle, m_uidTextBox);
		m_uidText.addKeyboardListener(new KeyboardListenerAdapter() {
			@Override
			public void onKeyDown(final Widget sender, final char keyCode, final int modifiers) {
				if (m_firstUidChangeAttempt) {
					m_firstUidChangeAttempt = false;
					m_listeners.fireUidChangeAttempt();
				}
			}
			@Override
			public void onKeyUp(final Widget sender, final char keyCode, final int modifiers) {
				if (keyCode == KEY_ENTER) {
					m_uid = m_uidText.getText();
					showPasswordPrompt();
					m_listeners.fireUidChangeAttempt();
					m_listeners.fireUidProvided(m_uid);
				}
			}
		});
		m_uidText.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				if (m_firstUidChangeAttempt) {
					m_firstUidChangeAttempt = false;
					m_listeners.fireUidChangeAttempt();
				}
			}
		});
		m_passwordText.addKeyboardListener(new KeyboardListenerAdapter() {
			@Override
			public void onKeyUp(final Widget sender, final char keyCode, final int modifiers) {
				if (keyCode == KEY_ENTER) {
					final String password = m_passwordText.getText();
					m_listeners.fireCredentialsProvided(m_uid, password);
				}
			}
		});
		final CellFormatter formatter = m_grid.getCellFormatter();
		formatter.setStyleName(0, 0, Style.LABEL);
		formatter.setStyleName(1, 0, Style.TEXT);
		showUidPrompt();
		initWidget(m_grid);
		setStyleName(WIDGET_ID);
	}

	public void reset() {
		m_uid = null;
		m_uidText.setText("");
		showUidPrompt();
	}

	public void setUid(final String uid) {
		m_uid = uid;
		m_uidText.setText(uid);
		showPasswordPrompt();
	}

	public void addCredentialsListener(final CredentialsListener listener) {
		if (m_listeners == null) {
			m_listeners = new CredentialsListenerCollection();
		}
		m_listeners.add(listener);
	}

	public void removeCredentialsListener(final CredentialsListener listener) {
		if (m_listeners != null) {
			m_listeners.remove(listener);
		}
	}

	public void showUidPrompt() {
		m_firstUidChangeAttempt = true;
		m_grid.setText(0, 0, CONSTANTS.username());
		m_grid.setWidget(1, 0, m_uidText);
		m_uidText.setFocus(true);
	}

	public void showPasswordPrompt() {
		m_passwordText.setText("");
		m_grid.setText(0, 0, CONSTANTS.password());
		m_grid.setWidget(1, 0, m_passwordText);
		m_passwordText.setFocus(true);
	}

	public void setEnabled(final boolean enabled) {
		m_passwordText.setEnabled(enabled);
		m_uidTextBox.setEnabled(enabled);
	}

	public boolean isEnabled() {
		return m_passwordText.isEnabled();
	}

	public void setFocus(final boolean focused) {
		((HasFocus) m_grid.getWidget(1, 0)).setFocus(focused);
	}

}
